﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.DDD.Code.Data
{
    public class SessionManager
    {
        private HttpContext _context;
        private Microsoft.Owin.IOwinContext _webContext;
		private NFinal.Owin.HtmlWriter writer;
        private IDictionary<string, string> cookies;
        private SessionInfo _sessionInfo;
        private string session_id;
        private NFinal.ServerType _serverType = NFinal.ServerType.UnKnown;
        public SessionManager()
        {
            this._serverType = NFinal.ServerType.IsStatic;
            _sessionInfo = new SessionInfo();
        }
        public SessionManager(Microsoft.Owin.IOwinContext context)
        {
            this._webContext = context;
            this._serverType = NFinal.ServerType.MicrosoftOwin;
            _sessionInfo = new SessionInfo();
        }
        public SessionManager(HttpContext context)
        {
            this._context = context;
            this._serverType = NFinal.ServerType.AspNET;
            _sessionInfo = new SessionInfo();
        }
        public SessionManager(string session_id)
        {
            this.session_id = session_id;
            this._serverType = NFinal.ServerType.UnKnown;
            _sessionInfo = new SessionInfo();
        }
		public SessionManager(NFinal.Owin.HtmlWriter writer,IDictionary<string, string> cookies)
        {
            this.writer = writer;
            this.cookies = cookies;
        }
		public object this[string key]
        {
            get
            {
                object obj = System.Runtime.Caching.MemoryCache.Default.Get(string.Format("s:{0}:{1}", SessionId, "DDD_"+key));
                return obj;
            }
            set
            {
                System.Runtime.Caching.MemoryCache.Default.Set(string.Format("s:{0}:{1}", SessionId, "DDD_"+key), value, DateTimeOffset.Now.AddMinutes(30));
            }
        }
        public string SessionId
        {
            get
            {
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["DDD_session_id"] != null)
                    {
                        this.session_id = _context.Request.Cookies["DDD_session_id"].Value;
                    }
                    else
                    {
                        this.session_id = Guid.NewGuid().ToString("N");
                        HttpCookie cookie = new HttpCookie("DDD_session_id", this.session_id);
                        _context.Response.Cookies.Add(cookie);
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["DDD_session_id"] != null)
                    {
                        this.session_id = _webContext.Request.Cookies["DDD_session_id"];
                    }
                    else
                    {
                        this.session_id = Guid.NewGuid().ToString("N");
                        _webContext.Response.Cookies.Append("DDD_session_id", this.session_id);
                    }
                }
				else if (this._serverType == NFinal.ServerType.NFinalOwin)
                {
                    if (cookies.ContainsKey("DDD_session_id"))
                    {
                        return cookies["DDD_session_id"];
                    }
                    else
                    {
                        this.session_id = Guid.NewGuid().ToString("N");
                        writer.SetCookie("DDD_session_id", this.session_id);
                    }
                }
                return this.session_id;
            }
            set
            {
                this.session_id = value;
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Response.Cookies["DDD_session_id"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("DDD_session_id", value);
                        _context.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        _context.Request.Cookies["DDD_session_id"].Value = value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["DDD_session_id"] != null)
                    {
                        _webContext.Response.Cookies.Delete("DDD_session_id");
                    }
                    _webContext.Response.Cookies.Append("DDD_session_id", value);
                }
				else if(this._serverType == NFinal.ServerType.NFinalOwin)
                {
                    writer.SetCookie("DDD_session_id", value);
                }
            }
        }
        public int userId
        {
            get 
			{
				object obj = System.Runtime.Caching.MemoryCache.Default.Get(string.Format("s:{0}:{1}", SessionId, "DDD_userId"));
                if (obj != null)
                {
						_sessionInfo.userId=(int)obj;
                }
                return _sessionInfo.userId;
            }
            set 
			{
				_sessionInfo.userId = value;
                System.Runtime.Caching.MemoryCache.Default.Set(string.Format("s:{0}:{1}", SessionId, "DDD_userId"), _sessionInfo.userId, DateTimeOffset.Now.AddMinutes(30));
            }
        }
    }
}